<?php
class Nadeb_Form_Select
{
	private $param;
	private $label;
	private $value;
	private $id;
	private $options;
	
	private $inputTag;
	private $tag = "";
	
	
	public function __construct($_param)
	{
		$this->label   = $_param["label"];
		$this->id      = $_param["id"];
		$this->options = $_param["options"];
		$this->param   = $_param;
		
		unset($this->param["type"]);
		unset($this->param["rules"]);
		unset($this->param["options"]);
		unset($this->param["label"]);
		
		if( isset( $_param["value"] ) )
		{
			$this->value = $_param["value"];
			unset($this->param["value"]);
		}
	}
	
	public function create_object()
	{
		/*
		 * input TAG
		 */
		$this->inputTag = "<select";
		foreach($this->param as $key => $value)
		{
			if( $key == "name" || $key == "value")
				$this->inputTag .= ' ' . $key . '="' . $value . '"';
			else
				$this->inputTag .= ' ' . $key . '="' . strtolower($value) . '"';
		}
		$this->inputTag .= ">";
		
		if( isset($this->value) )
		{
			if( isset($this->options[$this->value]) )
			{
				$this->inputTag .= "\n\t\t\t\t<option selected=\"selected\" value=\"{$this->value}\">{$this->options[$this->value]}</option>";
			}
			else
			{
				$this->inputTag .= "\n\t\t\t\t<option selected=\"selected\" value=\"{$this->value}\">{$this->value}</option>";
			}
		}
		else
		{
			$this->inputTag .= "\n\t\t\t\t<option selected=\"selected\" value=\"\"> - </option>";
		}
		
		foreach($this->options as $key => $value)
			$this->inputTag .= "\n\t\t\t\t<option value=\"{$key}\">{$value}</option>";
		
		$this->inputTag .= "\n\t\t\t</select>";
		
		/*
		 * dt TAG
		 */
		$this->tag .= "
		<dt id=\"{$this->id }-label\">
			<label for=\"".strtolower($this->param["name"])."\" class=\"{$this->param["name"]}-label\">{$this->label}</label>
		</dt>";

		
		/*
		 * dd TAG
		 */
		$this->tag .= "
		<dd id=\"{$this->id }-object\">
			$this->inputTag
		</dd>";
	}
	
	public function get_xhtml()
	{
		return $this->tag;
	}
}
